package models

import (
	"gorm.io/gorm"
	"time"
)

// Room 房间模型
type Room struct {
	gorm.Model
	Name        string     `json:"name" gorm:"size:100;not null"`
	CreatorID   int        `json:"creator_id" gorm:"not null"`
	CreatorName string     `json:"creator_name" gorm:"size:50"`
	Tag         string     `json:"tag" gorm:"size:20"`
	IsMatching  bool       `json:"is_matching" gorm:"default:false"`
	EmptySince  *time.Time `json:"empty_since"`
}

func CreateRoom(room *Room) error {
	return GetDB().Create(room).Error
}

func GetAllRooms() ([]Room, error) {
	var rooms []Room
	err := GetDB().Find(&rooms).Error
	return rooms, err
}

func DeleteRoomByID(id string) error {
	return GetDB().Delete(&Room{}, id).Error
}

func SetRoomEmptySince(id string, t time.Time) error {
	return GetDB().Model(&Room{}).Where("id = ?", id).Update("empty_since", t).Error
}

func ClearRoomEmptySince(id string) error {
	return GetDB().Model(&Room{}).Where("id = ?", id).Update("empty_since", nil).Error
}
